package mya_dc.shared_classes.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;



/**
 * A Get acrobat reader location dialog
 * 
 * @author	 Adam Levi
 * 			 MYA
 *
 */
public class GetAcrobatReaderDialog extends JDialog 
{
	private static final long serialVersionUID = 4479727066097132732L;
	private JTextField acrobatTextField;
	/**
	 * Create the dialog
	 */
	public GetAcrobatReaderDialog(String location) {
		super();
		setTitle("Get Acrobat window");
		getContentPane().setLayout(null);
		setBounds(100, 100, 294, 198);

		final MultiLabel acrobatReaderWasMultiLabel = new MultiLabel();
		acrobatReaderWasMultiLabel.setText("Acrobat reader was not found in the system, \nplease set the location of Acrobat reader.");
		acrobatReaderWasMultiLabel.setBounds(14, 10, 256, 61);
		getContentPane().add(acrobatReaderWasMultiLabel);

		acrobatTextField = new JTextField();
		acrobatTextField.setBounds(14, 77, 152, 20);
		getContentPane().add(acrobatTextField);

		final JButton browseButton = new JButton();
		browseButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				doBrowse();
			}
		});
		browseButton.setText("browse");
		browseButton.setBounds(175, 76, 95, 23);
		getContentPane().add(browseButton);

		final JButton button = new JButton();
		button.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				doOK();
			}
		});
		button.setText("OK");
		button.setBounds(177, 131, 95, 23);
		getContentPane().add(button);

		final JButton cancelButton = new JButton();
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				doCancel();
			}
		});
		cancelButton.setText("Cancel");
		cancelButton.setBounds(14, 131, 95, 23);
		getContentPane().add(cancelButton);
		//
		
		acrobatTextField.setText(location);
		canceled = true;
	}
	
	public String m_location;
	public boolean canceled;
	
	private void doError()
	{
		JOptionPane.showMessageDialog(this, "Error: Bad location");
	}
	
	private void doCancel()
	{
		m_location = null;
		this.dispose();
	}
	
	private void doOK()
	{
		canceled = false;
		this.dispose();
	}
	

	private void doBrowse()
	{
		String location = SharedComponents.getLocation(0,this);
		if (location == null || location.equals(""))
		{
			doError();
			return;
		}
		File f = new File(location);
		if (!f.exists() || (!f.getName().equals("AcroRd32.exe")))
		{
			doError();
			return;
		}
		m_location = location;
		acrobatTextField.setText(location);
	}
	

}
